import java.util.LinkedList;
import java.util.Queue;

public class T {



    public static void main(String[] args) {
        MyStack myStack=new MyStack();
        myStack.push(1);
        myStack.push(2);
        myStack.push(3);
        myStack.push(4);
        System.out.println(myStack.queue1.size());

       myStack.top();
    }
}

class MyStack {
    Queue<Integer> queue;
    Queue<Integer> queue1;

    public MyStack() {
        queue=new LinkedList<>();
        queue1=new LinkedList<>();


    }

    public void push(int x) {
        if(queue.isEmpty()){
            queue1.add(x);
        }else {
            queue.add(x);
        }

    }

    public int pop() {
        if(queue.isEmpty()) {

            int count=queue1.size()-1;

            for (int i = 0; i <count; i++) {
                queue.add(queue1.poll());

            }
            return queue1.poll();

        }else{
            int count=queue.size()-1;

            for (int i = 0; i <count; i++) {
                queue1.add(queue.poll());


            }
            return queue.poll();
        }





    }

    public int top() {
        if(queue.isEmpty()) {
            int count=queue1.size();

            for (int i = 0; i <count-1; i++) {
                queue.add(queue1.poll());

            }
            int n= queue1.poll();
            queue.add(n);

            return n;

        }else{
            int count=queue.size();

            for (int i = 0; i <count-1; i++) {
                queue1.add(queue.poll());


            }
            int n= queue.poll();
            queue1.add(n);
            return n;
        }




    }

    public boolean empty() {

        return queue1.isEmpty()&&queue.isEmpty();

    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */
